package com.baiyang.server.control.system;

import java.util.List;
import java.util.Map;

import com.baiyang.server.model.system.Role;
import com.baiyang.server.tools.DataTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.model.system.RoleJurisdiction;
import com.baiyang.server.service.system.RoleJurisdictionService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/roleJurisdictionOperation")
@Jurisdiction(value = "RoleJurisdictionCon")
public class RoleJurisdictionCon {
	@Autowired
	private RoleJurisdictionService roleJurisdictionService = null;
	
	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "批量读取")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<RoleJurisdiction> getPage(@RequestBody JSONObject object){
		Letter<RoleJurisdiction> letter = new Letter<RoleJurisdiction>();
		try {
			PageEntity<RoleJurisdiction> result = roleJurisdictionService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail(result.getResult());
			letter.setResult("数据获取成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件获取数据
	 *
	 * @return
	 */
	@Jurisdiction(value = "selectList")
	@RequestMapping(value = "/selectList", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<RoleJurisdiction> selectList(@RequestBody RoleJurisdiction roleJurisdiction) {
		Letter<RoleJurisdiction> letter = new Letter<RoleJurisdiction>();
		try {

			List<RoleJurisdiction> result = roleJurisdictionService.selectList(roleJurisdiction);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据条件获取一个权限角色关联信息
	 * @param roleJurisdiction
	 * @return
	 */
	@Jurisdiction(value = "读取")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<RoleJurisdiction> selectOne(@RequestBody RoleJurisdiction roleJurisdiction){
		Letter<RoleJurisdiction> letter = new Letter<RoleJurisdiction>();
		try {
			RoleJurisdiction obj = roleJurisdictionService.selectOne(roleJurisdiction);
			letter.setData(obj);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 插入数据
	 * @param roleJurisdiction
	 * @return
	 */
	@Jurisdiction(value = "新增")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody RoleJurisdiction roleJurisdiction){
		Letter<String> letter = new Letter<String>();
		roleJurisdiction.setId(DataTool.createUUID());
		Map<String, String> sub = SubjectNotNull.subject(roleJurisdiction);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！");
			return letter;
		}
		try {
			roleJurisdictionService.insert(roleJurisdiction);
			letter.setDetail("SUCCESS");
			letter.setResult("数据插入成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 批量插入数据
	 *
	 * @param object
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody JSONObject object) {
		Letter<String> letter = new Letter<String>();
		try {
			List<RoleJurisdiction> roleJurisdictions = object.getJSONArray("list").toJavaList(RoleJurisdiction.class);
			String roleId = object.getString("roleId");
			roleJurisdictionService.DelAndInsertBatch(roleJurisdictions,roleId);
			letter.setDetail("数据插入成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝条件进行删除
	 * @param roleJurisdiction
	 * @return
	 */
	@Jurisdiction(value = "删除")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody RoleJurisdiction roleJurisdiction){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(roleJurisdiction)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			roleJurisdictionService.delete(roleJurisdiction);
			letter.setDetail("SUCCESS");
			letter.setResult("数据获取成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 *
	 * @param roleJurisdictions
	 * @return
	 */
	@Jurisdiction(value = "deletes")
	@RequestMapping(value = "/deletes", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> deletes(@RequestBody List<RoleJurisdiction> roleJurisdictions) {
		Letter<String> letter = new Letter<String>();
		if (DataTool.isBlank(roleJurisdictions)) {
			letter.setResult("FAIL");
			letter.setDetail("请至少选中一行！");
			return letter;
		}
		try {
			roleJurisdictionService.deletes(roleJurisdictions);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除成功！");
		}
		return letter;
	}

	/**
	 * 根绝id更新
	 * @param roleJurisdiction
	 * @return
	 */
	@Jurisdiction(value = "修改")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody RoleJurisdiction roleJurisdiction){
		Letter<String> letter = new Letter<String>();
		if(roleJurisdiction.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			roleJurisdictionService.updateById(roleJurisdiction);
			letter.setDetail("SUCCESS");
			letter.setResult("数据获取成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;	
	}
}
